package com.listen.controller;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;

import com.listen.common.HttpClientUtil;

@WebServlet(urlPatterns = "/shuili", asyncSupported = true)
public class ShuiLi extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	private List<String> msgList;
	
	// 接收页面请求
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		Map<String,String> parmsMap = new ConcurrentHashMap<String,String>();
		msgList = new LinkedList<String>();
		String zy = request.getParameter("ZY");
		String txtUserName = request.getParameter("txtUserName");
		String txtPwd = request.getParameter("txtPwd");
		String numBegin = request.getParameter("numBegin");
		String numEnd= request.getParameter("numEnd");
		parmsMap.put("zy", zy);
		parmsMap.put("txtUserName", txtUserName);
		parmsMap.put("txtPwd", txtPwd);
		parmsMap.put("numBegin", numBegin);
		parmsMap.put("numEnd", numEnd);
		request.setAttribute("page", 1);
		request.setAttribute("msgList", msgList);
		request.getRequestDispatcher("/result.jsp").forward(request, resp);
	}
	
	//智能听课第三步：智能登录
	public void login(Map<String,String> parmsMap){
		String loginUrl = parmsMap.get("loginUrl");
		String txtUserName = parmsMap.get("txtUserName");
		String zy = parmsMap.get("zy");
		String se = parmsMap.get("se");
		Map<String, String> maps = new ConcurrentHashMap<String, String>();
        maps.put("__VIEWSTATE", parmsMap.get("_viewstate")); 
        maps.put("__VIEWSTATEGENERATOR", parmsMap.get("_viewstategenerator"));
        maps.put("__EVENTVALIDATION",parmsMap.get("_eventvalidation")); 
        maps.put("txtUserName",parmsMap.get("txtUserName"));
        maps.put("txtPwd",parmsMap.get("txtPwd"));
        maps.put("ddlType", "2");
        maps.put("btnLogin", "");
        maps.put("txtLoginName", ""); 
        maps.put("txtDM", "");
        maps.put("txtFR", ""); 
        maps.put("zhjg", "");
        maps.put("zhfr", "");
        if(se !=null && !"".equals(se.trim())){
        	maps.put("se", parmsMap.get("se"));
        }
        CloseableHttpResponse response = null;
        try {
        	HttpPost httpPost = new HttpPost(loginUrl);// 创建httpPost
        	httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        	httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        	httpPost.setHeader("DNT", "1");
        	httpPost.setHeader("Upgrade-Insecure-Requests", "1");
        	response = HttpClientUtil.getInstance().sendHttpPost(maps,httpPost);
        	int state = response.getStatusLine().getStatusCode();
        	if(state == 302){
        		System.out.println("登录成功! state="+state);
        		Header[] hearders = response.getHeaders("Set-Cookie");
        		String loginCookie = hearders[0].getValue();
        		String sessionId = loginCookie.substring(0, loginCookie.indexOf(";") + 1);
        		String userIdCookie = hearders[1].getValue();
        		String userid = userIdCookie.substring(0, userIdCookie.indexOf(";") + 1);
        		String infoCookies = hearders[2].getValue();
        		String info = infoCookies.substring(0, infoCookies.indexOf(";") + 1);
        		//设置登录信息
        		parmsMap.put("sessionId", sessionId);
        		parmsMap.put("userid", userid);
        		parmsMap.put("info", info);
        		//听课
        		//updateStudyVideo(parmsMap);
        	}else{
        		print("登录失败！！！当前账户-"+txtUserName+"-所选职业为:"+zy+",请检查职业是否匹配!");
        	}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			HttpClientUtil.getInstance().closeResource(response);
		}
	}
	
	public void print(String msg){
		msgList.add(msg);
		System.out.println(msg);
	}

}
